package com.tenwives.ashop.kernel;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 特价促销
 */
@AllArgsConstructor
@Data
public class Special implements Predicate<Goods>, Function<Goods, Double> {
    Fruit fruit;
    Discount discount;
    LocalDateTime start;
    LocalDateTime end;

    @Override
    public Double apply(Goods goods) {
        assert goods != null;
        assert goods.fruit != null;
        assert fruit != null;
        assert discount != null;
        double total = goods.getTotal();
        if (test(goods)) {
            return discount.apply(total);
        }
        return total;
    }

    @Override
    public boolean test(Goods goods) {
        assert goods != null;
        assert goods.fruit != null;
        assert fruit != null;
        return goods.fruit.equals(fruit) && (start == null || LocalDateTime.now().isAfter(start)) && (end == null || LocalDateTime.now().isBefore(end));
    }
}
